package org.carabiner.infopanel;

import java.awt.Container;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.JScrollBar;

/**
 * This rule looks for controls (like CheckBoxes and Buttons) that have no
 * associated actions or action listeners. This rule cannot reliably check
 * Scroll Bars, because scroll bar buttons are sometimes intentionally left
 * unconnected.
 * 
 * @author Ben Rady (benrady@gmail.com)
 * 
 */
public class UnconnectedControlRule extends AbstractRule
{
  public String getName()
  {
    return "Unconnected Component";
  }

  protected List inspectAbstractButton(AbstractButton button)
  {
    List warnings = new ArrayList(1);
    if (isUnconnected(button))
    {
      warnings.add(createWarning(button));
    }
    return warnings;
  }

  public List inspectContainer(Container container)
  {
    if (!(container instanceof JScrollBar))
    {
      return super.inspectContainer(container);
    }
    return Collections.EMPTY_LIST;
  }

  private boolean isUnconnected(AbstractButton button)
  {
    return button.getAction() == null
        && button.getActionListeners().length == 0;
  }

  public String getDescription()
  {
    return "This component has no Actions or ActionListeners attached to it.";
  }
}
